import { requestClient } from '#/api/request';

export namespace TaskApi {
  /** 任务列表查询参数 */
  export interface TaskListParams {
    page?: number;
    per_page?: number;
    search?: string;
    status?: 'inactive' | 'active';
    service_type?: string;
    start_date?: string;
    end_date?: string;
    sort_by?: string;
    sort_order?: 'asc' | 'desc';
  }

  /** 任务记录接口 */
  export interface TaskRecord {
    id: number; // 改为 number
    tenant_id: number; // 改为 number
    name: string;
    service_type: string;
    status: 'inactive' | 'active';
    start_time: string;
    end_time: string;
    settlement_cycle: string; // 新增
    min_fee: string; // 改为 string，保持后端格式
    max_fee: string; // 改为 string，保持后端格式
    billing_unit: string; // 新增 计费单位
    gender_requirement: string; // 新增
    education_requirement: string; // 新增
    min_age: number; // 新增
    max_age: number; // 新增
    work_location: string;
    work_time: string; // 新增
    work_content: string; // 改为必需字段
    work_compensation: string; // 新增
    work_benefits: string; // 新增
    qr_code_url: string | null; // 新增
    created_at: string;
    updated_at: string; // 新增
    applications: any[]; // 新增，具体类型待定义
    applications_count?: number; // 保留作为可选字段
  }

  /** 分页响应结构 */
  export interface TaskListResponse {
    data: TaskRecord[];
    current_page: number;
    per_page: string | number; // 改为 string，因为后端返回的是字符串
    total: number;
    last_page: number;
    from: number;
    to: number;
    // 新增 Laravel 分页器的完整字段
    first_page_url: string;
    last_page_url: string;
    next_page_url: string | null;
    prev_page_url: string | null;
    path: string;
    links: Array<{
      url: string | null;
      label: string;
      active: boolean;
    }>;
  }
  /** API响应包装 */
  export interface ApiResponse<T> {
    success: boolean;
    data: T;
    message?: string;
  }

  /** 创建任务请求体 */
  export interface CreateTaskPayload {
    service_type: string;
    name: string;
    start_time: string;
    end_time: string;
    settlement_cycle: string;
    min_fee: number | string;
    max_fee: number | string;
    billing_unit: string;
    gender_requirement: string;
    education_requirement: string;
    min_age: number;
    max_age: number;
    work_location: string;
    work_content: string;
    work_compensation: string;
    work_time: string;
    work_benefits?: string | null;
  }

  export interface TaskQrCodeResponse {
    task_id: string;
    task_name: string;
    qr_url: string;
    qr_image_base64: string;
  }
}

/**
 * 获取任务列表
 */
export async function getTaskListApi(params: TaskApi.TaskListParams) {
  return requestClient.get<TaskApi.TaskListResponse>(`/api/tenant/tasks`, {
    params,
  });
}
/**
 * 获取任务详情
 */
export async function getTaskDetailApi(id: string | number) {
  return requestClient.get<TaskApi.TaskRecord>(`/api/tenant/tasks/${id}`);
}

/**
 * 创建任务
 */
export async function createTaskApi(data: TaskApi.CreateTaskPayload) {
  return requestClient.post<TaskApi.TaskRecord>('/api/tenant/tasks', data);
}

/**
 * 更新任务
 */
export async function updateTaskApi(id: string | number, data: TaskApi.CreateTaskPayload) {
  return requestClient.put<TaskApi.TaskRecord>(`/api/tenant/tasks/${id}`, data);
}

export async function updateTaskStatusApi(id: string | number, status: 'active' | 'inactive') {
  return requestClient.post<TaskApi.TaskRecord>(`/api/tenant/tasks/${id}/status`, { status });
}

/**
 * 删除任务
 */
export async function deleteTaskApi(id: string) {
  return requestClient.delete<TaskApi.ApiResponse<null>>(`/api/tenant/tasks/${id}`);
}

export async function getTaskQrCodeApi(id: string | number) {
  return requestClient.get<TaskApi.TaskQrCodeResponse>(`/api/tenant/tasks/${id}/qr-code`);
}
